package net.contrend.system.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import net.contrend.system.dao.MenuDao;
import net.contrend.system.form.MenuForm;
import net.contrend.util.BaseAction;

public class MenuAction extends BaseAction {
    
    private MenuDao menuDao;
    
    private void initDao() {
        if (null == this.menuDao) {
            this.menuDao = (MenuDao) this.getBean("menuDao");
        }
    }
    
    public MenuDao getMenuDao() {
        this.initDao();
        return menuDao;
    }

    public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        this.initDao();
        
        DynaActionForm df = (DynaActionForm) form;
        MenuForm menu = (MenuForm) df.get(this.ACTION_FORM_NAME);
        int i = this.menuDao.insert(menu);
        request.setAttribute("ACTION_RESULT", i);
        
        return mapping.findForward(this.ACTION_PAGE_EDIT);
    }
    
    public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        this.initDao();
        
        DynaActionForm df = (DynaActionForm) form;
        MenuForm menu = (MenuForm) df.get(this.ACTION_FORM_NAME);
        int i = this.menuDao.update(menu);
        request.setAttribute("ACTION_RESULT", i);
        
        return mapping.findForward(this.ACTION_PAGE_EDIT);
    }
    
    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        this.initDao();
        
        String menuId = request.getParameter("menuId");
        int i = this.menuDao.delete(menuId);
        response.getWriter().write(String.valueOf(i));
        
        return null;
    }
    
    public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        this.initDao();
        
        String menuId = request.getParameter("menuId");
        DynaActionForm df = (DynaActionForm) form;
        df.set(this.ACTION_FORM_NAME, this.menuDao.query(menuId));
        
        List<MenuForm> menuChildren = this.menuDao.getMenuChildren(menuId, false);
        request.setAttribute("menuChildren", menuChildren);
        
        return mapping.findForward(this.ACTION_PAGE_EDIT);
    }
    
    public ActionForward manage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        this.initDao();
        
        List<MenuForm> menus = this.menuDao.getMenus(false);
        request.setAttribute("menus", menus);
        return mapping.findForward("manage");
    }
    
}
